package com.xbs.controller.base;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map.Entry;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 通用Servlet
 * 
 * @author Administrator
 *
 */
public class BaseServlet extends HttpServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private String contentPath; // 内容路径
	private ThreadLocal<HttpServletRequest> req = new ThreadLocal<>(); // 请求对象
	private ThreadLocal<HttpServletResponse> resp = new ThreadLocal<>(); // 响应对象
	private final String PARAM_KEY = "_paramKey_"; // 参数常量
	private String jspPath;//请求转发的jsp路径
	@SuppressWarnings("unchecked")
	@Override
	protected final void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		try {
			
			// 设置参数
			this.contentPath = request.getContextPath(); // 获取根目录
			// 设置编码
			request.setCharacterEncoding("utf-8");
			response.setCharacterEncoding("utf-8");
			// 将内容路径保存到请求作用域中
			request.setAttribute("cxt", this.contentPath);
			//获取jsp请求的路径
			jspPath=request.getParameter("jspPath");
			req.set(request);
			resp.set(response);
			
			// 处理重定向参数
			// 获取session中的重定向参数集合

			HashMap<String, Object> params = (HashMap<String, Object>) request.getSession()
					.getAttribute(this.PARAM_KEY);
			// 如果结婚后不为空,则读取集合中的数据
			if (params != null && !params.isEmpty()) {
				// 遍历集合中的数据
				for (Entry<String, Object> e : params.entrySet()) {
					// 将数据保存到请求作用域
					request.getSession().setAttribute(e.getKey(), e.getValue());
				}
				// 删除session中的数据
				//request.getSession().removeAttribute(this.PARAM_KEY);
			}
			super.service(request, response);
		} catch (Exception e) {
			throw new ServletException(e);
		}
		
	}

	@Override
	public final void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.service(request, response);
	}

	/**
	 * 转发
	 * 
	 * @param viewName
	 *            页面名字
	 * @throws ServletException
	 * @throws IOException
	 */
	public final void forward(String viewName) throws ServletException, IOException {
		// 处理转发
		
		req.get().getRequestDispatcher("/WEB-INF/jsp/" + viewName + ".jsp").forward(req.get(), resp.get());
	}
	public final void forward() throws ServletException, IOException{
		
		if(jspPath!=null){	
			req.get().getRequestDispatcher("/WEB-INF/jsp/"+jspPath+".jsp").forward(req.get(), resp.get());
		}
	}
	/**
	 * 重定向
	 * 
	 * @param uri
	 *            资源路径
	 * @throws IOException
	 */
	public final void redirect(String uri) throws IOException {
		// 处理重定向
		resp.get().sendRedirect(this.contentPath + uri);
	}

	/**
	 * 重定向 (带参数)
	 * 
	 * @param uri
	 * @param params
	 * @throws IOException
	 */
	public final void redirect(String uri, HashMap<String, Object> params) throws IOException {
		// 将参数保存到session中
		req.get().getSession().setAttribute(this.PARAM_KEY, params);
		this.redirect(uri);
	}
}
